import {
	getHitList,
	getLyricsData,
	getMusicBanner,
	getRecommendSong,
	getSongInfo,
} from "../../service/api_music"

export default {
	namespaced: true,
	state: {
		banners: [],
		hitLists: [],
		recommendPlaylist: [],
		recommendSongs: [],
		newRanking: '',
		originRanking: '',
		upRanking: '',
		peakList: [],
		listDetail: '',
		songInfo: [],
		lyrics: ''
	},
	mutations: {
		sendBanner(state, payload) {
			state.banners = payload
		},
		sendRecommendSongs(state, payload) {
			state.recommendSongs = payload
		},
		sendHitLists(state, payload) {
			state.hitLists = payload
		},
		sendRecommendPlaylist(state, payload) {
			state.recommendPlaylist = payload
		},
		sendNewRanking(state, payload) {
			state.newRanking = payload
		},
		sendOriginRanking(state, payload) {
			state.originRanking = payload
		},
		sendUpRanking(state, payload) {
			state.upRanking = payload
		},
		sendPeakList(state, payload) {
			if (payload != '' && state.peakList.length < 3) {
				state.peakList.push(payload)
			}
		},
		sendListDetail(state, payload) {
			state.listDetail = payload
		},
		sendSongInfo(state, payload) {
			state.songInfo = payload
		},
		sendLyrics(state, payload) {
			state.lyrics = payload
		}
	},
	actions: {
		async fetchBannerData(context, payload) {
			await getMusicBanner().then(res => {
				context.commit('sendBanner', res.banners)
			}).catch(error => {
				console.log(error);
			})
		},
		// 热门歌单
		async fetchHitListsData(context, payload) {
			await getHitList().then(res => {
				context.commit('sendHitLists', res.playlists)
			})
		},
		// 推荐歌单
		async fetchRecommendPlaylistData(context, payload) {
			await getHitList('华语').then(res => {
				context.commit('sendRecommendPlaylist', res.playlists)
			})
		},
		// 推荐歌曲
		async fetchRecommendSongData(context, payload) {
			await getRecommendSong(3778678).then(res => {
				const lists = res.playlist.tracks.slice(0, 6)
				context.commit('sendRecommendSongs', lists)
			}).catch(error => {
				console.log(error);
			})
		},
		async fetchNewRanking(context, payload) {
			// 新歌榜
			await getRecommendSong(3779629).then(res => {
				const lists = res.playlist
				context.commit('sendNewRanking', lists)
				if (payload != 0) {
					context.commit('sendPeakList', lists)
				}
			}).catch(error => {
				console.log(error);
			})
		},
		async fetchOriginRanking(context, payload) {
			// 原创榜
			await getRecommendSong(2884035).then(res => {
				const lists = res.playlist
				context.commit('sendOriginRanking', lists)
				if (payload != 0) {
					context.commit('sendPeakList', lists)
				}
			}).catch(error => {
				console.log(error);
			})
		},
		async fetchUpRanking(context, payload) {
			// 飙升榜
			await getRecommendSong(19723756).then(res => {
				const lists = res.playlist
				context.commit('sendUpRanking', lists)
				if (payload != 0) {
					context.commit('sendPeakList', lists)
				}
			}).catch(error => {
				console.log(error);
			})
		},
		// 歌单详情
		async fetchListDetail(context, payload) {
			await getRecommendSong(payload).then(res => {
				context.commit('sendListDetail', res.playlist)
			}).catch(error => {
				console.log(error);
			})
		},
		// 歌曲信息
		async fetchSongInfo(context, payload) {
			await getSongInfo(payload).then(res => {
				context.commit('sendSongInfo', res.songs[0])
			}).catch(error => {
				console.log(error);
			})
		},
		// 歌词
		async fetchLyrics(context, payload) {
			await getLyricsData(payload).then(res => {
				context.commit('sendLyrics', res.lrc.lyric)
			}).catch(error => {
				console.log(error);
			})
		}
	}
}
